home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 12800 < prev    next >
Encoding:
Text File  |  1996-08-05  |  1.6 KB  |  50 lines

  1. Path: druid.borland.com!usenet
  2. From: pete@borland.com (Pete Becker)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Help! Newbie question ...
  5. Date: 21 Mar 1996 16:35:30 GMT
  6. Organization: Borland International
  7. Message-ID: <4is0gi$in8@druid.borland.com>
  8. References: <4ilkfu$41q@Kaos.deepcove.com>
  9. NNTP-Posting-Host: pbecker.borland.com
  10. Mime-Version: 1.0
  11. Content-Type: Text/Plain; charset=ISO-8859-1
  12. X-Newsreader: WinVN 0.99.5
  13.  
  14. In article <4ilkfu$41q@Kaos.deepcove.com>, saffleck@deepcove.com says...
  15. >
  16. >Is this legal ? 
  17. >
  18. >class A {...};
  19. >
  20. >A f();
  21. >
  22. >main() {
  23. >  A a = f();
  24. >}
  25. >
  26. >A f()
  27. >{
  28. >   A fa;
  29. >   return fa;
  30. >}
  31. >
  32. >        Can you create an object on the stack within a function and then 
  33. return 
  34. >the object ?  I know that objects created on the stack are deleted at block or
  35. > function exit, so is it ok to return them and use the value to initialize 
  36. >another object?  
  37. >        I realize that there are ways to get around this such as dynamically 
  38. >allocating the object and returning a pointer or passing the function an 
  39. >object which the function can initialize in whatever way it wants.  However, 
  40. >I'd like to know if this can be done 'cus it seems more tidy since the 
  41. >returned object is automaticly deleted on block exit and can therefore be 
  42. >ignored if not needed.  I'd also like to know for my own personal interest. 
  43.  
  44. Yes, it's legal. It works the same way as returning a built-in type: the 
  45. compiler is responsible for generating code that handles copying the object to 
  46. its destination. You are responsible for providing an assignment operator and 
  47. copy constructor that work correctly if the compiler-generated ones won't do 
  48. what you need.
  49.  
  50.